Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudflare): pass raw body instead of parsing it #629

Merged
merged 1 commit into from
Nov 7, 2022
Merged

fix(cloudflare): pass raw body instead of parsing it #629

merged 1 commit into from
Nov 7, 2022

Conversation

brendonmatos
Copy link
Contributor

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

In cases where it is necessary to read the body of the raw request, the cloudflare deploy was always parsing the body even using useRawBody.
Implementations like the stripe's webhook that needs to read the raw body would break.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@pi0
Copy link
Member

pi0 commented Nov 3, 2022

Hi @brendonmatos. Thanks for PR. Would you please provide a reproduction or a better example? We might fix the useRequestBody utility instead as it respects the mime type to support blob and formdata as well (.txt() only works for text and json)

@brendonmatos
Copy link
Contributor Author

brendonmatos commented Nov 3, 2022

In my case the useRequestBody was doing its job correctly.

The useRawBody returns the parsed body instead of raw when the localCall is invoked with the parsed body (it wasn't clear to me how or why this happens).

const r = await nitroApp.localCall({
event,
url: url.pathname + url.search,
host: url.hostname,
protocol: url.protocol,
headers: Object.fromEntries(event.request.headers.entries()),
method: event.request.method,
redirect: event.request.redirect,
body
})

To support blobs and formdata an option can be to pass the buffer forward using cloudflare's event.request.arrayBuffer() and converting to buffer, like this:

body = Buffer.from(await event.request.arrayBuffer())

@pi0 What do you think about this?

@pi0
Copy link
Member

pi0 commented Nov 3, 2022

Buffer for workers is supported via lots of unenv polyfills. If possible, it would be better to use arrayBuffer directly and handle it from h3 utility. (We already depend on Buffer.concat here)

Seems a good idea!

@brendonmatos
Copy link
Contributor Author

thanks for the path you gave me!
the behaviour was implemented in both entries

@barisbora
Copy link

@pi0
Copy link
Member

pi0 commented Nov 7, 2022

Thanks!

@pi0
Copy link
Member

pi0 commented Nov 7, 2022

Can we pass CF records too?

Another enhancement, but sure we shall pass them to event.context.cf

@pi0 pi0 changed the title fix(cloudflare): raw body instead parsing (unjs#589) fix(cloudflare): pass raw body instead of parsing it Nov 7, 2022
@pi0 pi0 merged commit 837f894 into unjs:main Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants